package com.xp.ai.net;


import com.xp.ai.service.Assistant;
import com.xp.ai.util.ApiKey;
import com.xp.ai.util.ModelUtils;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.request.ChatRequest;
import dev.langchain4j.model.chat.request.ChatRequestParameters;
import dev.langchain4j.rag.DefaultRetrievalAugmentor;
import dev.langchain4j.rag.content.retriever.WebSearchContentRetriever;
import dev.langchain4j.rag.query.router.DefaultQueryRouter;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.web.search.WebSearchEngine;
import dev.langchain4j.web.search.tavily.TavilyWebSearchEngine;

import java.time.Duration;

/***
 * 使用联网插件
 * 使用 TavilyWeb
 * @author xp
 */
public class WithNetChat {

    public static void main(String[] args) {

        //构建联网搜索引擎
        WebSearchEngine webSearchEngine = TavilyWebSearchEngine.builder()
                .apiKey(ApiKey.TAVILY_KEY)
                .timeout(Duration.ofSeconds(10L))
                .build();
        WebSearchContentRetriever webSearchContentRetriever = WebSearchContentRetriever.builder()
                .webSearchEngine(webSearchEngine)
                .maxResults(5)
                .build();

        DefaultQueryRouter queryRouter = new DefaultQueryRouter(webSearchContentRetriever);
        DefaultRetrievalAugmentor defaultRetrievalAugmentor = DefaultRetrievalAugmentor.builder()
                .queryRouter(queryRouter)
                .build();

        ChatLanguageModel chatLanguageModel = ModelUtils.getHuoshanv3Model();

        Assistant assistant = AiServices.builder(Assistant.class)
                .chatLanguageModel(chatLanguageModel)
                .retrievalAugmentor(defaultRetrievalAugmentor)
                .build();

//        String chat = assistant.chat("成都今天的PM2.5 是多少？");
        String chat = assistant.chat("what time is today?");
        System.out.println(chat);

    }
}
